export default class SkuUtil {
  static handleLimitInteger(e) {
    const key = e.key
    if (key === ' ') {
      e.preventDefault()
      return
    }
    if (key !== 'Tab' && key !== 'Backspace' && !e.metaKey && key !== 'ArrowLeft' && key !== 'ArrowRight') {
      if (!/[0-9]/.test(key)) {
        e.preventDefault()
      }
    }
  }

  static handleLimitFloat(e) {
    const key = e.key
    if (key === ' ') {
      e.preventDefault()
      return
    }
    if (key !== 'Tab' && key !== 'Backspace' && !e.metaKey && key !== 'ArrowLeft' && key !== 'ArrowRight') {
      const value = e.target.value
      if (isNaN(value + '' + key)) {
        e.preventDefault()
      }
    }
  }

  static handleLimitFloatPrecision(e, precision = 2) {
    if (e.target.value !== '') {
      const pow = Math.pow(10, precision)
      e.target.value = Math.round(parseFloat(e.target.value) * pow) / pow
    }
  }

  static validateSkuEditor(rule, value, callback) {
    const msg = value.getValidateErrorMsg()
    if (msg) {
      callback(msg)
    } else {
      callback()
    }
  }
}
